package com.ruoyi.system.outto.game.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.model.domain.AccountInfo;
import com.ruoyi.system.model.domain.InfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.game.SLAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/14 15:28
 */
@Slf4j
@Service
public class SLAPIImpl implements SLAPI {

    @Resource
    private InfoImgMapper infoImgMapper;
    @Autowired
    private CommonApi commonApi;

    @Value("${mima.baseUrl}")
    private String MMMBaseUrl;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;

    @Override
    public AjaxResult addAndEditAndDelGame(AccountInfo accountInfo, String type, SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(accountInfo, type,salePartner);
        params.put("supplier",salePartner.getSaleKey());
        params.put("Method","Gamereceive");
        String paramsStr = JSONObject.toJSONString(params);
        if("1".equals(type)) {
            log.info(salePartner.getName() + "添加/编辑接口请求参数：" + paramsStr);
        }else{
            log.info( salePartner.getName() + "删除接口请求参数：" + paramsStr);
        }
        String response = HttpUtils.sendPost(salePartner.getOther(), paramsStr, "application/json");
        if("1".equals(type)) {
            log.info(salePartner.getName() + "添加/编辑接口返回参数：" + response);
        }else{
            log.info(salePartner.getName() + "删除接口返回参数：" + response);
        }
        JSONObject resultJson = JSONObject.parseObject(response);

        Map<String, Object> result = new HashMap<>();
        if (!"200".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("msg"));
        return AjaxResult.success(result);
    }

    private Map<String, Object> params(AccountInfo accountInfo, String type,SalePartner sa) {

        Map<String, Object> params = new HashMap<>();
        JSONObject dataJson = new JSONObject();
        dataJson.put("id",accountInfo.getId());
        if (accountInfo.getDes().length() > 90){
            dataJson.put("title",descTitle+accountInfo.getId()+ " " + accountInfo.getDes().substring(0,90));
        }else {
            dataJson.put("title",descTitle+accountInfo.getId()+ " " + accountInfo.getDes());
        }

        BigDecimal bigDecimal = commonApi.addPrice(accountInfo.getPrice(),  sa.getSeleep());
        commonApi.setPrice(bigDecimal,accountInfo.getId(), sa);

        dataJson.put("bprice",bigDecimal);
        dataJson.put("price",bigDecimal);
        dataJson.put("createtime",accountInfo.getCreateTime());
        dataJson.put("creator",salePartner.getSaleKey());
        dataJson.put("count",0);
        dataJson.put("state" , type);
        if (accountInfo.getDes().length() > 90){
            dataJson.put("bk3",accountInfo.getDes().substring(0,90));
        }else {
            dataJson.put("bk3",accountInfo.getDes());
        }
        String game = accountInfo.getType();
        if (Objects.equals("CF端游",game)){
            game = "穿越火线";
        }
        if (Objects.equals("CF手游",game)){
            game = "枪战王者";
        }
        dataJson.put("gamename",game);
        dataJson.put("areaname",accountInfo.getRegional());
        InfoImg infoImg = new InfoImg();
        infoImg.setInfoId(accountInfo.getId());
        infoImg.setType(Constants.STR_1);
        List<InfoImg> infoImgs = infoImgMapper.selectInfoImgList(infoImg);
        String pic1 = "";
        if (Objects.nonNull(accountInfo.getTitleImg())){
            pic1 = accountInfo.getTitleImg() + ",";
        }
        if (!CollectionUtils.isEmpty(infoImgs)) {
            pic1 = pic1 + infoImgs.stream().map(i -> i.getImgUrl()).collect(Collectors.joining(","));
        } else {
            pic1 =  pic1 + LCConstant.defaultImg;
        }
        dataJson.put("pic1",pic1);
        dataJson.put("pic2",accountInfo.getTitleImg());
        dataJson.put("updatetime",accountInfo.getUpdateTime());
        params.put("DataJson", JSONObject.toJSONString(dataJson));
        return params;
    }

    public static void main(String[] args) {

        String url = "https://pre.mimaapp.com/v5/goods/query";

        Map<String,Object> param = new HashMap<>();
        param.put("appid","761544930");
        param.put("secret","lcmSitAiEMWhnLHtdGRcoZmErNbRy599");
        List<Long> ids = new ArrayList<>();
        ids.add(1886l);
        ids.add(2100l);
                //1886,2100
        param.put("ids",ids);

        String response = HttpUtils.sendPost(url, JSONObject.toJSONString(param), "application/json");

        System.out.println(response);

    }
}
